﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;

namespace ExcelAddIn1
{
    public partial class TestSelectForm : Form
    {
        private class ListItem
        {
            public String TestName { get; set; }
            public int TestID { get; set; }
        }

        private int testID;
        private double alpha = 0.05;
        private double meanDifference = 0.0;
        
        public TestSelectForm()
        {
            InitializeComponent();
            testID = -1;

            testBox.DisplayMember = "TestName";
            testBox.ValueMember = "TestID";

            testBox.Items.Add(new ListItem { TestName = "F-test for equal variance", TestID = 1});
            testBox.Items.Add(new ListItem { TestName = "T-test: Two-sided independent", TestID = 2});
            testBox.Items.Add(new ListItem { TestName = "T-test: One-sided independent", TestID = 3});
            testBox.Items.Add(new ListItem { TestName = "T-test: Two-sided paired", TestID = 4});
            testBox.Items.Add(new ListItem { TestName = "T-test: One-sided paired", TestID = 5});
            testBox.Items.Add(new ListItem { TestName = "ANOVA: one-way", TestID = 6});
            testBox.Items.Add(new ListItem { TestName = "Chi-square test for independence", TestID = 7});
            testBox.Items.Add(new ListItem { TestName = "Correlation coefficient", TestID = 8});
            testBox.Items.Add(new ListItem { TestName = "Linear Regression", TestID = 9});
            testBox.Items.Add(new ListItem { TestName = "Wilcoxon signed-rank test", TestID = 10});
            testBox.Items.Add(new ListItem { TestName = "Wilcoxon rank-sum test", TestID = 11});
            testBox.Items.Add(new ListItem { TestName = "Kruskal-Wallis", TestID = 12});
            testBox.Items.Add(new ListItem { TestName = "Jarque-Bera test", TestID = 13});
            testBox.Items.Add(new ListItem { TestName = "Confidence Intervals", TestID = 14});
            testBox.Items.Add(new ListItem { TestName = "Spearman rank correlation coefficient", TestID = 15});
            testBox.Items.Add(new ListItem { TestName = "Moving Average, simple", TestID = 16});
            testBox.Items.Add(new ListItem { TestName = "Moving Average, weighted", TestID = 17});
            testBox.Items.Add(new ListItem { TestName = "Exponential Smoothing", TestID = 18});
            testBox.Items.Add(new ListItem { TestName = "Exponential Smoothing with Trend Adjustment", TestID = 19});
            testBox.Items.Add(new ListItem { TestName = "T-test: Single population for the mean, two-sided", TestID = 20});
            //END

            System.Windows.Forms.ToolTip t1 = new System.Windows.Forms.ToolTip();
            t1.SetToolTip(this.alphaLabel, "Significance (alpha) level for a test, usually 0.05 or 0.10.");

            System.Windows.Forms.ToolTip t2 = new System.Windows.Forms.ToolTip();
            t2.SetToolTip(this.meanLabel, "Assumed difference in means between two datasets. Set to 0 if\nyou are only testing if there is a difference in means.");

            alphaBox.Text = alpha + "";
        }

        public int GetTestID()
        {
            return testID;
        }

        public double GetAlpha()
        {
            return alpha;
        }

        public double GetMeanDifference()
        {
            return meanDifference;
        }

        private void runTestBT_Click(object sender, EventArgs e)
        {
            ListItem l = (ListItem)testBox.SelectedItem;
            testID = l.TestID;
            
            try
            {
                alpha = Convert.ToDouble(alphaBox.Text);
            }
            catch (FormatException)
            {
                alpha = 0.05;
            }

            try
            {
                meanDifference = Convert.ToDouble(meanBox.Text);
            }
            catch (FormatException)
            {
                meanDifference = 0.0;
            }

            this.Dispose();
        }

        private void cancelBT_Click(object sender, EventArgs e)
        {
            testID = -1;
            this.Dispose();
        }

        private void testBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListItem l = (ListItem)testBox.SelectedItem;
            Test t = Test.GetTest(l.TestID, null);

            //Check which parameter fields to enable
            if (t.EnableMeanDiffField())
            {
                meanLabel.Visible = true;
                meanBox.Visible = true;
            }
            else
            {
                meanLabel.Visible = false;
                meanBox.Visible = false;
            }
            if (t.EnableAlphaField())
            {
                alphaLabel.Visible = true;
                alphaBox.Visible = true;
            }
            else
            {
                alphaLabel.Visible = false;
                alphaBox.Visible = false;
            }

            //Set test info text
            infoBox.Text = t.GetInfo();
        }

        private void TestSelectForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Dispose();
        }
    }
}
